/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.toedo.controllers.DataController;
import org.toedo.gui.translations.Messages;

public class SaveProjectAction extends Action {
	
	private boolean mAskForName;
	
	public SaveProjectAction(boolean askForName) {
		mAskForName = askForName;
	}
	
	public void run() {
		if ((!DataController.getInstance().hasFileName()) ||
				(mAskForName)) {
			Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			
			FileDialog dialog = new FileDialog(shell, SWT.SAVE);
		    dialog.setFilterExtensions(new String[] {"*.xml", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
		    dialog.setFilterNames(new String[] {Messages.SaveAction_XMLFilterLabel, Messages.AllFilesFilter});
		    dialog.setOverwrite(true);
		    
		    String fileSelected = dialog.open();
		    
		    if ((fileSelected != null) &&
		    		(!fileSelected.isEmpty())) {
		    	DataController.getInstance().setFileName(fileSelected);
		    	DataController.getInstance().save();
		    }
		} else {		
			DataController.getInstance().save();
		}
	}

}
